<template>
    <div class="divBox">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <div class="container">
            <el-form ref="form" inline :model="form">
              <el-form-item label="关键字：">
                <el-input v-model="form.keywords" placeholder="请输入关键字" class="selWidth"  size="small" clearable>
                  <el-button slot="append" size="small" icon="el-icon-search" @click="handlerSearch" v-hasPermi="['platform:city:list']"/>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <el-button type="primary"  size="small" @click="addCity" v-hasPermi="['platform:city:add']">新增分站</el-button>
        </div>
        <el-table
          v-loading="loading"
          :data="tableData.list"
          :header-cell-style=" {fontWeight:'bold'}"
        >
          <el-table-column
            prop="id"
            label="ID"
            min-width="80"
          />
          <el-table-column
            label="账号"
            min-width="150"
            prop="account"
          />
          <el-table-column
            label="城市行政编码"
            min-width="120"
            prop="areaCode"
          />
          <el-table-column
            label="城市名称"
            min-width="100"
            prop="city"
          />
          <el-table-column
            label="账号是否独立"
            min-width="120"
            prop="independent"
            align="center"
          >
          <template slot-scope="scope">
            <!--  0:否 1:是 -->
            <el-tag v-if="scope.row.independent === 1">是</el-tag>
            <el-tag v-else type="info">否</el-tag>
          </template>
          </el-table-column>
          <el-table-column
            label="最后登录IP"
            min-width="100"
            prop="lastIp"
          />
          <el-table-column
            label="登录次数"
            min-width="100"
            prop="loginCount"
            align="center"
          />
          <el-table-column
            label="手机号"
            min-width="120"
            prop="phone"
          />
          <el-table-column
            label="真实姓名"
            min-width="100"
            prop="realName"
          />

          <el-table-column
            label="分站名称"
            min-width="150"
            prop="name"
          />
          <el-table-column
            label="是否开启"
            min-width="100"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                class="demo"
                :active-value="true" 
                :inactive-value="false"
                active-text="开启"
                inactive-text="关闭"
                @change="bindStatus(scope.row)"
                v-if="checkPermi(['platform:city:close','platform:city:open'])"
              />
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            min-width="180"
            align="center"
            label="操作"
          >
            <template slot-scope="scope">
                <el-button type="text" size="small" @click="onDetail(scope.row)" v-hasPermi="['platform:city:detail']">详情</el-button>
                <el-button type="text" size="small" @click="resetPassword(scope.row)" v-hasPermi="['platform:city:reset:password']">重置密码</el-button>
              <el-button type="text" size="small" @click="onEdit(scope.row)" v-hasPermi="['platform:city:update']">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>`
        <div class="block-pagination">
          <el-pagination
            :page-sizes="[20, 40, 60, 80]"
            :page-size="tableData.limit"
            :current-page="tableData.page"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.total"
            @current-change="pageChange"
            @size-change="handleSizeChange"
          />
        </div>
      </el-card>
      <EditForm ref="editForm" />
      <ResetPasswrod ref="resetPassword" />
      <DetailBox ref="detailBox" />
    </div>
  </template>
  
  <script>
  import * as cityApi from '@/api/city.js'
  import { checkPermi } from "@/utils/permission"; // 权限判断函数
  import EditForm from './components/edit-form/index.vue'
  import ResetPasswrod from './components/reset-password/index.vue'
  import DetailBox from './components/detail-box/index.vue'
  export default {
    name: 'CompanyList',
    components: { EditForm, ResetPasswrod, DetailBox },
    data() {
      return {
        constants:this.$constants,
        // 表单
        formConf: { fields: [] },
        form: {
          keywords: ''
        },
        tableData: {},
        page: 1,
        limit: 20,
        loading: false,
        fromType: 'add',
        formData: {
          status: false
        },
        isCreate: 0,
        formShow: false,
        editId: 0,
      }
    },
    created() {
      this.getCityList()
    },
    methods: {
      checkPermi,
      handlerSearch() {
        this.page = 1
        this.getCityList()
      },
      //  获取分站列表
      getCityList() {
        this.loading = true
        cityApi.getCityList({
          page: this.page,
          limit: this.limit,
          keywords: this.form.keywords
        }).then(res => {
          this.loading = false
          this.tableData = res
        }).catch(()=>{
          this.loading = false
        })
      },
      // 分站开关
      bindStatus(item) {
        cityApi.updateStatus({
          id: item.id,
          status: item.status,
        }).then(res => {
          this.$message.success('操作成功')
          this.getCityList()
        }).catch(() => {
          this.$message.error('修改失败!')
        })
      },
      // 分页
      pageChange(e) {
        this.page = e
        this.getCityList()
      },
      handleSizeChange(e) {
        this.limit = e
        this.getCityList()
      },
      // 添加分站
      addCity() {
       this.$refs.editForm.add()
      },
      // 删除分站
      bindDelete(item) {
        this.$modalSure().then(() => {
          cityApi.expressDelete({ id: item.id }).then(res => {
            this.$message.success('删除成功')
            this.getCityList()
          })
        })
      },
      // 编辑
      onEdit(row) {
        this.$refs.editForm.edit(row)
      },
      // 重置密码
      resetPassword(row) {
        this.$refs.resetPassword.edit(row)
      },
      // 详情
      onDetail(row) {
        this.$refs.detailBox.open(row)
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
    .selWidth{
      width: 350px;
    }
    .el-icon-plus {
      margin-right: 5px;
    }
  
    .demo .el-switch__label {
      position: absolute;
      display: none;
      color: #fff;
    }
  
    /*打开时文字位置设置*/
    .demo .el-switch__label--right {
      z-index: 1;
    }
  
    /*关闭时文字位置设置*/
    .demo .el-switch__label--left {
      z-index: 1;
      left: 19px;
    }
  
    /*显示文字*/
    .demo .el-switch__label.is-active {
      display: block;
    }
  
    .demo.el-switch .el-switch__core,
    .el-switch .el-switch__label {
      width: 60px !important;
    }
    .formBox{
      .el-input-number--medium{
        width: 100px;
      }
    }
  </style>
  